<?php
namespace Common\Model;
use Think\Model;
/**
 * 文件上传model
 */
class UploadsModel extends Model
{
  private $_obj = '';
  private $_uploadImgData = '';
  private $rootPath = '';
  public function __construct(){
    $this->_obj = new \Think\Upload();
    $path = $this->rootPath = C('qiniuPath')?C('qiniuPath'):'./upload/';
    $this->_obj->rootPath = './upload/';
    $this->_obj->subName = date(Y) . '/' . date(m) .'/' . date(d);
  }
  private function qiniuUpload($name,$path){
    $obj =new \Think\Qiniu();
    return $obj->upload($name,$path);
  }
  public function upload() {
        $res = $this->_obj->upload();
        if($res) {
            return C('rootPath') . $res['Filedata']['savepath'] . $res['Filedata']['savename'];
        }else{
            return false;
        }
    }

    public function imageUpload() {
        $res = $this->_obj->upload();
        $path = './upload/'. $res['Filedata']['savepath'] . $res['Filedata']['savename'];
        if($res) {
          if(C('QINIU_UPLOAD') ==true){
            $result = $this->qiniuUpload($res['Filedata']['savename'],$path);
            if($result['status'] == true){
              return $result['file_url'];
            }
          }else{
            return __ROOT__.'/upload/'. $res['Filedata']['savepath'] . $res['Filedata']['savename'];
          }
        }else{
            return false;
        }
    }
}
